package cn.iocoder.yudao.module.promotion.combination.api;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.promotion.combination.CombinationRecordApi;
import cn.iocoder.yudao.module.api.promotion.combination.dto.CombinationRecordCreateReqDTO;
import cn.iocoder.yudao.module.api.promotion.combination.dto.CombinationRecordCreateRespDTO;
import cn.iocoder.yudao.module.api.promotion.combination.dto.CombinationRecordRespDTO;
import cn.iocoder.yudao.module.api.promotion.combination.dto.CombinationValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.combination.convert.CombinationActivityConvert;
import cn.iocoder.yudao.module.promotion.combination.dal.dataobject.CombinationRecordDO;
import cn.iocoder.yudao.module.promotion.combination.service.CombinationRecordService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

/**
 * 拼团活动 API 实现类
 *
 * @author HUIHUI
 */
@Service
@Validated
public class CombinationRecordApiImpl implements CombinationRecordApi {

    @Resource
    private CombinationRecordService combinationRecordService;

    @Override
    public void validateCombinationRecord(Long userId, Long activityId, Long headId, Long skuId, Integer count) {
        combinationRecordService.validateCombinationRecord(userId, activityId, headId, skuId, count);
    }

    @Override
    public CombinationRecordCreateRespDTO createCombinationRecord(CombinationRecordCreateReqDTO dto) {
        return CombinationActivityConvert.INSTANCE.convert4(combinationRecordService.createCombinationRecord(dto));
    }

    @Override
    public CombinationRecordRespDTO getCombinationRecordByOrderId(Long userId, Long orderId) {
        CombinationRecordDO record = combinationRecordService.getCombinationRecord(userId, orderId);
        return BeanUtils.toBean(record, CombinationRecordRespDTO.class);
    }

    @Override
    public CombinationRecordRespDTO getCombinationRecordById(Long id) {
        CombinationRecordDO record = combinationRecordService.getCombinationRecordById(id);
        return BeanUtils.toBean(record, CombinationRecordRespDTO.class);
    }

    @Override
    public Map<Long, CombinationRecordRespDTO> getCombinationRecordsByIds(Collection<Long> ids) {
        List<CombinationRecordDO> records = combinationRecordService.getCombinationRecordByIds(ids);
        if (CollUtil.isEmpty(records)) {
            return Collections.emptyMap();
        }
        return convertMap(records, CombinationRecordDO::getId,
                record -> BeanUtils.toBean(record, CombinationRecordRespDTO.class));
    }

    @Override
    public CombinationValidateJoinRespDTO validateJoinCombination(Long userId, Long activityId, Long headId, Long skuId, Integer count) {
        return combinationRecordService.validateJoinCombination(userId, activityId, headId, skuId, count);
    }

}
